package com.deepfos.outreach.component;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.deepfos.outreach.domain.CashFlowPushDto;
import com.deepfos.outreach.domain.CashFlowSlot;
import com.deepfos.outreach.util.HttpUtil;
import com.yomahub.liteflow.core.NodeComponent;
import com.yomahub.liteflow.entity.data.Slot;
import lombok.extern.slf4j.Slf4j;
import okhttp3.Response;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * 付款计划推送
 */
@Slf4j
@Component("cashFlowPushCmp")
public class CashFlowPushCmp extends NodeComponent {

    private String url = "http://localhost:8088/outreach/sychCashFlow";

    @Override
    public void process() throws Exception {
        CashFlowSlot slot = this.getSlot();
        List<CashFlowPushDto> cfpds = slot.getCfpds();

        /***这里Mock付款计划推送***/
        push(cfpds, slot);

    }

    private void push(List<CashFlowPushDto> cfpds, Slot slot) throws IOException {
        String response = HttpUtil.pushJSONBody(JSON.toJSONString(cfpds), url, null, null);
        if (Objects.nonNull(response)) {
            JSONObject jsonObject = JSONObject.parseObject(response);
            if (Objects.nonNull(jsonObject) && "0".equals(jsonObject.get("code"))) {
                log.info(String.format("更新付款计划状态为 ：%s", "7"));
            }
        } else {
            log.info(String.format("更新付款计划状态为 ：%s", "2"));
        }
        Object responseData = slot.getResponseData();
        if (Objects.nonNull(responseData) && responseData instanceof Map) {
            ((Map<String, Object>) responseData).put("code", "0");
        }
    }

    @Override
    public boolean isAccess() {
        //过滤出优惠信息列表中有没有满减这个活动，如果有，则进入这个组件，反义就不进入
        CashFlowSlot slot = this.getSlot();
        if (Objects.nonNull(slot.getCfpds())) {
            return true;
        } else {
            return false;
        }
    }


}
